function DriverCtrl($scope, $http, $cookieStore) {
	$scope.DRIVER_TYPE = DRIVER_TYPE;
	$scope.DRIVER_ALGORITHM = DRIVER_ALGORITHM;
	
	$scope.saveDriver = function() {
		$scope.driverForm.submited = true;
		
		if ($scope.driverForm.$invalid) {
			return false;
		}
		
		if ($scope.mode == 'create') {
			$scope.editingDriver.providerId = $scope.selectedProvider.originalObject.id;
		}
		
		$http.post("/driver/save", $scope.editingDriver).success(function(response) {
			if (response.success) {
				$scope.backPage();
			} else {
				if (response.result == 101) {
					// 101: driver name duplicated
					$scope.driverForm.name.$setValidity('duplicated', false);
				} else {
					$scope.driverForm.globalErrorMsg = response.message || "保存驱动失败！";
				}
			}
		});
	};
	
	$scope.backPage = function() {
		window.location.href = "/driver/index?cookie=true";
	};
	
	$scope.init = function() {
		if (DRIVER_ID) {
			$scope.mode = 'update';
		} else {
			$scope.mode = 'create';
		}
		
		if ($scope.mode == 'update') {
			$http.post("/driver/get/" + DRIVER_ID).success(function(response) {
				if(response.success) {
					$scope.editingDriver = response.data || {};
				} else {
					Message.error("错误", response.message || "加载数据失败！");
				}
			});
		}
		
		$scope.$watch('editingDriver.name', function(newValue, oldValue) {
			$scope.driverForm.name.$setValidity('duplicated', true);
		});
	};
 
	$scope.init();
};

app.controller('DriverCtrl', DriverCtrl);